package com.soilflow.web.controller.auction;

import com.soilflow.common.constant.MqConstants;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.LandAuction;
import com.soilflow.common.domain.LandInfo;
import com.soilflow.common.domain.dto.LandAuctionDto;
import com.soilflow.common.domain.dto.LandAuctionEnrollDto;
import com.soilflow.common.domain.vo.LandAuctionEnrollVo;
import com.soilflow.common.domain.vo.LandAuctionVo;
import com.soilflow.common.domain.vo.LandInfoVo;
import com.soilflow.info.service.LandAuctionService;
import com.soilflow.info.service.LandInfoService;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 竞拍
 */
@RequestMapping("/auction/info")
@RestController
@Log4j2
public class LandAuctionController extends BaseController {


    @Autowired
    private LandAuctionService landAuctionService;
    @Autowired
    private LandInfoService landInfoService;




    /**
     * 获取竞拍土地名称
     * @param dto
     * @return
     */
    @GetMapping("/landName")
    public AjaxResult getLandInfoByName(LandAuctionDto dto){
        List<String> landNames = landAuctionService.getLandInfoByName(dto);
        return AjaxResult.success(landNames);
    }

    /**
     * 获取土地信息名称
     * @param dto
     * @return
     */
    @GetMapping("/landInfo/landName")
    public AjaxResult getLandNameFormLandInfo(LandAuctionDto dto){
        List<LandAuctionVo>  landNames = landAuctionService.getLandNameFormLandInfo(dto);
        return AjaxResult.success(landNames);
    }

    /**
     * 竞拍信息列表
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getLandAuctionInfo(LandAuctionDto dto){
        startPage();
        List<LandAuctionVo> list = landAuctionService.getLandAuctionInfo(dto);
        SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (LandAuctionVo landAuctionVo : list) {
            try {
                landAuctionVo.setApplyStart(sdft.format(sdft.parse(landAuctionVo.getApplyStart())));
                landAuctionVo.setApplyEnd(sdft.format(sdft.parse(landAuctionVo.getApplyEnd())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return getDataTable(list);
    }

    /**
     * 竞拍信息列表(不分页)
     * @return
     */
    @GetMapping
    public AjaxResult getInfo(LandAuctionDto dto){
        List<LandAuctionVo> list = landAuctionService.getLandAuctionInfo(dto);

        return AjaxResult.success(list);
    }





    /**
     * 添加竞拍信息
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public AjaxResult saveLandAuctionInfo(@RequestBody LandAuctionDto dto) throws ParseException {
        //校验报名时间
        if (dto.getApplyStart().compareTo(dto.getAuctionStart()) >0 || dto.getApplyEnd().compareTo(dto.getAuctionStart()) >0){
            return AjaxResult.error("报名时间应在开始竞拍时间之前");
        }
        //校验延迟发布时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Long currentTime = System.currentTimeMillis();
        Long applyStartTime = sdf.parse(dto.getApplyStart()).getTime();
        Long applyEndTime = sdf.parse(dto.getApplyEnd()).getTime();
        Long auctionStartTime = sdf.parse(dto.getAuctionStart()).getTime();
        Long auctionEndTime = sdf.parse(dto.getAuctionEnd()).getTime();

        if (currentTime > applyStartTime || currentTime > applyEndTime
                || currentTime > auctionStartTime || currentTime > auctionEndTime){
            return AjaxResult.error("设置的时间早于当前时间");
        }


        Long applyStartResultTime = applyStartTime - currentTime;
        Long applyEndTimeResultTime = applyEndTime - currentTime;
        Long auctionStartResultTime = auctionStartTime - currentTime;
        Long auctionEndResultTime = auctionEndTime - currentTime;



        if (applyStartResultTime > Integer.MAX_VALUE || applyEndTimeResultTime > Integer.MAX_VALUE
                || auctionStartResultTime > Integer.MAX_VALUE || auctionEndResultTime > Integer.MAX_VALUE){
            return AjaxResult.error("延迟时间设置过长");
        }

        dto.setApplyStartDelayTime(Integer.parseInt(applyStartResultTime.toString()));
        dto.setApplyEndDelayTime(Integer.parseInt(applyEndTimeResultTime.toString()));
        dto.setAuctionStartDelayTime(Integer.parseInt(auctionStartResultTime.toString()));
        dto.setAuctionEndDelayTime(Integer.parseInt(auctionEndResultTime.toString()));

        // 修改土地状态
        Long infoId = dto.getInfoId();
        LandInfo byId = landInfoService.getById(infoId);
        byId.setStatus("1");
        landInfoService.updateById(byId);


        Boolean result = landAuctionService.saveLandAuctionInfo(dto);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error("请勿重复添加");
    }


    /**
     * 删除竞拍信息
     * @param ids
     * @return
     */
    @DeleteMapping("/del/{ids}")
    public AjaxResult delLandAuctionInfo(@PathVariable  List<Long> ids){
        landAuctionService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 回显详情
     * @return
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getAuctionInfoById(@PathVariable Long  id){
        LandAuctionVo vo = landAuctionService.getAuctionInfoById(id);
        return AjaxResult.success(vo);

    }


    /**
     * 更新竞拍信息
     * @param dto
     * @return
     */
    @PutMapping("/update")
    public AjaxResult updateAuctionInfo(@RequestBody LandAuctionDto dto){
        landAuctionService.updateAuctionInfo(dto);
        return AjaxResult.success();
    }

}
